define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	require("resource/js/component/backSpace.js");
	var AJAX = require("resource/js/component/ajax.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var ECombobox = require("resource/js/component/easycombobox.js");
	ECombobox.create({
		url:BASEPATH+"/base/authoriza/basecode/getType.htm", param:{checkeds:"login_exit_flag,pageNum"}
	});
	var Obj = window.loadI18n('base/user');
	var dgutil1 = undefined, dgutil2 = undefined;
	var config = {
		columnStacs:[[
	         {field:'userId',title:""+Obj.get('base.common.success.user')+" ID",width:160,align:'left',height:50},
	         {field:'userName',title:""+Obj.get('base.common.success.user')+ Obj.get('base.common.success.name')+"",width:120,align:'center',height:50},
	         {field:'memo',title:""+Obj.get('base.user.login')+ Obj.get('base.common.success.time')+"",width:87,align:'left',height:50, 
	        	 formatter: function(val, row, index) {
	        		 return '<a class="showDetail" href="javascript:void(0);" userId="'+row.userId+'" title='+Obj.get("base.common.success.Check")+'>'+val+'</a>';
	        	 }
	         }
         ]], 
         columnDetail:[[
	         {field:'userId',title:""+Obj.get('base.common.success.user')+" ID",width:50,align:'left',height:50},
	         {field:'userName',title:""+Obj.get('base.common.success.user')+ Obj.get('base.common.success.name')+"",width:50,align:'left',height:50},
	         {field:'logintime',title:""+Obj.get('base.user.login')+ Obj.get('base.common.success.time')+"",width:80,align:'left',height:50},
	         {field:'exittime',title:""+Obj.get('base.common.success.Cancel')+ Obj.get('base.common.success.time')+"",width:80,align:'left',height:50},
	         {field:'ip',title:"IP"+Obj.get('base.common.success.address')+"",width:60,align:'center',height:50},
	         {field:'mac',title:"MAC"+Obj.get('base.common.success.address')+"",width:60,align:'center',height:50,hidden:true},
	         {field:'exitflag',title:""+Obj.get('base.common.success.Cancel')+ Obj.get('base.common.success.way')+"",width:50,align:'center',height:50},
	         {field:'deviceType',title:""+Obj.get('base.common.success.Devicetype')+"",width:300,align:'left',height:50},
	         {field:'memo',title:""+Obj.get('base.common.success.note')+"",width:100,align:'left',height:50,hidden:true}
		]]
	}
	var dgCfg1 = undefined, dgCfg2 = undefined;
	function initpage(){
		$('#dg').datagrid(dgCfg1);
		ECombobox.init({ ele:"#pageNum", grouptype:"pageNum",value:"50"}); 
		ECombobox.init({ ele:"#exitflag", grouptype:"login_exit_flag" });
	}
	
	function dateCheck() {
		var stime = $('#logintimeStart').datetimebox('getValue'), 
			etime = $('#logintimeEnd').datetimebox('getValue');
		if (stime > etime) {
			$.messager.show({title:""+Obj.get('base.common.success.Systemprompt')+"",msg:""+Obj.get('base.common.success.loginless')+""});
			return false;
		}
		stime = $('#exittimeStart').datetimebox('getValue'), 
		etime = $('#exittimeEnd').datetimebox('getValue');
		if (stime > etime) {
			$.messager.show({title:""+Obj.get('base.common.success.Systemprompt')+"",msg:""+Obj.get('base.common.success.cancelless')+""});
			return false;
		}
		return true;
	}
	
	var createChart = function(data){
		var catagroy = [],xdata=[],ydata=[];
		for(var index in data.rows){
			catagroy.push(data.rows[index]);
			xdata.push(data.rows[index].userName);
			ydata.push(data.rows[index].memo);
		}
		
		var option={
				title: {  
                    //主标题文本，'\n'指定换行  
                    text: ""+Obj.get('base.common.success.statistics')+"",  
                    //水平安放位置，默认为左侧，可选为：'center' | 'left' | 'right' | {number}（x坐标，单位px）  
                    x: 'left',  
                    //垂直安放位置，默认为全图顶端，可选为：'top' | 'bottom' | 'center' | {number}（y坐标，单位px）  
                    y: 'top'  
                },
              //提示框，鼠标悬浮交互时的信息提示  
                tooltip: {  
                    //触发类型，默认（'item'）数据触发，可选为：'item' | 'axis'  
                    trigger: 'axis',
                    showDelay : 0,
                    formatter : function (params) {
                    	if(params!=null){
                    		return params[0].name + ':'+ params[0].value+"次" ;
                    	}else{
                    		return;
                    	}
                    }
                },
                legend: {
                   x : 'center',
                   y : 'bottom',
                   data:[""+Obj.get('base.common.success.loginnum')+""]
                },
                toolbox: {
                   show : true,
                   feature : {
                	   mark : {show: true},
                       dataZoom : {show: true},
                       dataView : {show: true, readOnly: false},
                       restore : {show: true},
                       saveAsImage : {show: true}
                    }
                },
                xAxis : [
                         {
                            type : 'category',
                            data:xdata
                        }
                    ],
                    yAxis : [
                        {
                            type : 'value'
                        }
                    ],
                    series : [
                        {
                            name:""+Obj.get('base.common.success.loginnum')+"",
                            type:'scatter',
                            data: ydata,
                            markPoint : {
                                data : [
                                    {type : 'max', name:""+Obj.get('base.common.success.max')+""},
                                    {type : 'min', name:""+Obj.get('base.common.success.min')+""}
                                ]
                            }
                        }
                    ]
                };
		var chartContainer = document.getElementById('chart');
		
		//用于使chart自适应高度和宽度,通过窗体高宽计算容器高宽
		var resizeChartContainer = function () {
			var outWidth = ($(window).width())-($('#div1').width())-50;
			var outHeight = ($(window).height())-($('#searchform').height())-120;
			chartContainer.style.width = outWidth+'px';
			chartContainer.style.height = outHeight+'px';
		};
		//设置容器高宽
		resizeChartContainer();
		
		var myChart = echarts.init(chartContainer);
		//option.height = document.body.offsetHeight - document.forms[0].offsetHeight;
		myChart.setOption(option);
		//用于使chart自适应高度和宽度
		window.onresize = function () {
		    //重置容器高宽
			resizeChartContainer();
		    myChart.resize();
		};
	}
	
	var grid = function(){
		AJAX.init().success(function(code,msg,data){
			$('#dg').datagrid("loadData",data);
			createChart(data);
			
		}).error(function(code,msg,data){
		}).post(BASEPATH+'/base/authoriza/login/queryLogStatistics.htm',$("#searchform").serializeJson());
	}
	
	module.exports = {
		init:function(){
			var btnStacs ={ 
					title:{
						_class:"",
						text:""+Obj.get('base.common.success.stat')+""
					},
					btn:[{
				    	iconCls: 'icon-search',
				    	url:'/base/authoriza/login/queryLogStatistics.htm',
				    	text:"查询",
				    	handler: function(){
				    		if (dateCheck()) {
				    			grid();
				    		}
				    	}
					}]
				}, 
				btnBack = { 
						title:{
							_class:"",
							text:""+Obj.get('base.common.success.stat')+"-"+Obj.get('base.common.success.details')+""
						},
						btn:[{
					    	iconCls: 'icon-search',
					    	text:""+Obj.get('base.common.success.query')+"",
					    	handler: function() {
					    		if (dateCheck()) {
						    		dgutil2.pager(1);
					    		}
					    	}
						},{
					    	iconCls: 'icon-back',
					    	text:""+Obj.get('base.common.success.back')+"",
					    	handler: function() {
								$("#searchUserId").textbox("setValue", "");
								$("#hidetwo").hide();
								$("#hideone").show();
								$("#num").show();
								grid();
					    	}
						}]
					};
			dgCfg1 = {
					fitColumns:true,
					singleSelect:false,
					striped:true,
					toolbar:btnStacs,
					columns:config.columnStacs,
					eid:"dg",
					fit:true,
					url:BASEPATH+'/base/authoriza/login/queryLogStatistics.htm',
					args:"searchform",
					loadFilter:function(data){
						if (data.data){
							return data.data;
						} else {
							return data;
						}
					},
					onLoadSuccess: function(data){
						if(data.total>0){
							createChart(data);
							$("a.showDetail").on("click", function() {
								var userId = $(this).attr("userId");
								$("#searchUserId").textbox("setValue", userId);
								$("#num").hide();
								$("#hideone").hide();
								$("#hidetwo").show();
								dgutil2 = new EUtil.ajaxDataGrid(dgCfg2);
							})
						}
						var pbody = $(this).datagrid("getPanel").find(".datagrid-view2>.datagrid-body");
					}
			};
			dgCfg2 = {
					fitColumns:true,
					singleSelect:false,
					striped:true,
					autoRowHeight:true,
					toolbar:btnBack,
					columns:config.columnDetail,
					eid:"dg1", 
					url:BASEPATH+'/base/authoriza/login/queryLoginLog.htm',
					args:"searchform"
			};
			using(["datagrid","parser","messager","dialog","combobox", "tab"],function(){
				initpage();
			});
		}
	}
});